
package com.phevos.usercenter.client.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.phevos.usercenter.client.UserCenterConstant;
import com.phevos.usercenter.client.vo.FunctionsValue;
import com.phevos.usercenter.client.vo.ObjFunctionValue;
import com.phevos.usercenter.client.vo.OrganizationValue;
import com.phevos.usercenter.client.vo.RelGroupFunctionValue;
import com.phevos.usercenter.client.vo.RelGroupObjFunctionValue;
import com.phevos.usercenter.client.vo.RelOrgFunctionValue;
import com.phevos.usercenter.client.vo.RelOrgObjFunctionValue;
import com.phevos.usercenter.client.vo.RelOrgUserValue;
import com.phevos.usercenter.client.vo.RelUserFunctionValue;
import com.phevos.usercenter.client.vo.RelUserGroupValue;
import com.phevos.usercenter.client.vo.RelUserObjFunctionValue;
import com.phevos.usercenter.client.vo.UserGroupValue;
import com.phevos.usercenter.client.vo.UserValue;

/**
 * @author hjh
 */
public final class Function
{

    public static CacheManager cacheManager = CacheManager.getInstance();

    private final static Logger logger = LoggerFactory.getLogger(Function.class.getName());

    /**
     * ��֤����Ȩ��
     * 
     * @param userAccount
     * @param functionName
     * @return
     */
    public static boolean validateUserFunction(final String userAccount, final String functionName)
    {
        Cache userCache = cacheManager.getCache(UserCenterConstant.USERCACHE);
        Cache functionCache = cacheManager.getCache(UserCenterConstant.FUNCTIONSCACHE);
        Cache userGroupCache = cacheManager.getCache(UserCenterConstant.USERGROUPCACHE);
        Cache organizationCache = cacheManager.getCache(UserCenterConstant.ORGANIZATIONCACHE);
        if (userCache == null)
        {
            logger.error("UserCache cache hadn't defined in ehcahe.xml ");
            return false;
        }
        if (functionCache == null)
        {
            logger.error("functionCache cache hadn't defined in ehcahe.xml ");
            return false;
        }
        if (userGroupCache == null)
        {
            logger.error("userGroupCache cache hadn't defined in ehcahe.xml ");
            return false;
        }
        if (organizationCache == null)
        {
            logger.error("organizationCache cache hadn't defined in ehcahe.xml ");
            return false;
        }

        List ufList = getCache(functionCache, UserCenterConstant.RELUSERFNCTION);
        List userList = getCache(userCache, UserCenterConstant.USER);
        List funcList = getCache(functionCache, UserCenterConstant.FUNCTIONS);

        Integer userId = getUserIdByAccount(userList, userAccount);

        Integer funcId = null;
        for (int i = 0; i < funcList.size(); i++)
        {
            FunctionsValue funcValue = (FunctionsValue)funcList.get(i);
            if (funcValue.getName() != null && funcValue.getName().equals(functionName))
            {
                funcId = funcValue.getId();
                break;
            }
        }
        if (userId == null || funcId == null)
        {
            return false;
        }
        // userFunction
        for (int i = 0; i < ufList.size(); i++)
        {
            RelUserFunctionValue rel = (RelUserFunctionValue)ufList.get(i);
            if (rel != null && rel.getFuncId() != null && rel.getUserId() != null && userId != null && rel.getFuncId().equals(funcId)
                    && rel.getUserId().equals(userId))
            {
                return true;
            }
        }

        // groupFunction
        List ugList = getCache(userGroupCache, UserCenterConstant.RELUSERGROUP);
        for (Iterator iter = ugList.iterator(); iter.hasNext();)
        {
            RelUserGroupValue userGroup = (RelUserGroupValue)iter.next();
            if (userGroup != null && userGroup.getUserId() != null && userGroup.getUserId().equals(userId))
            {
                Integer groupId = userGroup.getGroupId();
                List gfList = getCache(functionCache, UserCenterConstant.RELGROUPFUNCTION);
                for (Iterator iterator = gfList.iterator(); iterator.hasNext();)
                {
                    RelGroupFunctionValue groupFunction = (RelGroupFunctionValue)iterator.next();
                    if (groupFunction.getFuncId() != null && groupFunction.getGroupId() != null && groupFunction.getFuncId().equals(funcId)
                            && groupFunction.getGroupId().equals(groupId))
                    {
                        return true;
                    }
                }
            }
        }

        // orgFunction
        List ouList = getCache(organizationCache, UserCenterConstant.ORGUSER);
        for (Iterator iter = ouList.iterator(); iter.hasNext();)
        {
            RelOrgUserValue orgUser = (RelOrgUserValue)iter.next();
            if (orgUser != null && orgUser.getUserId() != null && orgUser.getUserId().equals(userId))
            {
                Integer orgId = orgUser.getOrgId();
                List ofList = getCache(organizationCache, UserCenterConstant.RELORGFUNCTION);
                for (Iterator iterator = ofList.iterator(); iterator.hasNext();)
                {
                    RelOrgFunctionValue orgFunction = (RelOrgFunctionValue)iterator.next();
                    if (orgFunction.getFuncId() != null && orgFunction.getOrgId() != null && orgFunction.getFuncId().equals(funcId)
                            && orgFunction.getOrgId().equals(orgId))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * ��֤������Ȩ��
     * 
     * @param userAccount
     * @param functionName
     * @return
     */

    // resultMap.put(""+userAccount+"_"+objId+"_"+objType+"_"+funcName, "true");
    public synchronized static boolean validateUserObjFunction(String userAccount, String functionName, String objId, String obj_type)
    {
        HashMap existMap = getUserObjMap();
        String key = userAccount + "_" + objId + "_" + obj_type + "_" + functionName;
        if (existMap != null && existMap.containsKey(key))
        {
            return true;
        }
        return false;

        // Cache userCache =
        // cacheManager.getCache(UserCenterConstant.USERCACHE);
        // Cache functionCache = cacheManager
        // .getCache(UserCenterConstant.FUNCTIONSCACHE);
        // Cache userGroupCache = cacheManager
        // .getCache(UserCenterConstant.USERGROUPCACHE);
        // Cache organizationCache = cacheManager
        // .getCache(UserCenterConstant.ORGANIZATIONCACHE);
        // Cache objFunctionCache =
        // cacheManager.getCache(UserCenterConstant.OBJFUNCTIONCACHE);
        //		
        //		
        // if (userCache == null) {
        // logger.fatal("UserCache cache hadn't defined in ehcahe.xml ");
        // return false;
        // }
        // if (functionCache == null) {
        // logger.fatal("functionCache cache hadn't defined in ehcahe.xml ");
        // return false;
        // }
        // if (userGroupCache == null) {
        // logger.fatal("userGroupCache cache hadn't defined in ehcahe.xml ");
        // return false;
        // }
        // if (organizationCache == null) {
        // logger
        // .fatal("organizationCache cache hadn't defined in ehcahe.xml ");
        // return false;
        // }
        // Integer objFuncId = null;
        // List ofList =
        // getCache(objFunctionCache,UserCenterConstant.OBJFUNCTION);
        // for (Iterator iter = ofList.iterator(); iter.hasNext();) {
        // ObjFunctionValue objfunction = (ObjFunctionValue) iter.next();
        // if (functionName!= null && objfunction.getName()!=null &&
        // objfunction.getName().equals(functionName)) {
        // objFuncId = objfunction.getId();
        // break;
        // }
        // }
        // List userList = getCache(userCache,UserCenterConstant.USER);
        // Integer userId = getUserIdByAccount(userList, userAccount);
        //		
        // //getUserObjFunc
        // List uofList =
        // getCache(objFunctionCache,UserCenterConstant.RELUSEROBJFUNCTION);
        // for (int i = 0; i < uofList.size(); i++) {
        // RelUserObjFunctionValue userObj = (RelUserObjFunctionValue)
        // uofList.get(i);
        // if (userObj != null && userObj.getFuncId()!= null
        // && userObj.getUserId() != null
        // && userObj.getFuncId().equals(objFuncId)
        // && userObj.getUserId().equals(userId)
        // && userObj.getObjId().equals(new Integer(Integer.parseInt(objId)))
        // && userObj.getObjType().equals(new
        // Integer(Integer.parseInt(obj_type)))) {
        // return true;
        // }
        // }
        // //getGroupObjFunc
        // List ugList = getCache(userGroupCache,
        // UserCenterConstant.RELUSERGROUP);
        // for (Iterator iter = ugList.iterator(); iter.hasNext();) {
        // RelUserGroupValue userGroup = (RelUserGroupValue) iter.next();
        // if (userGroup != null && userGroup.getUserId() != null
        // && userGroup.getUserId().equals(userId)) {
        // Integer groupId = userGroup.getUserId();
        // List gofList =
        // getCache(objFunctionCache,UserCenterConstant.RELGROUPOBJFUCNTION);
        // for (Iterator iterator = gofList.iterator(); iterator.hasNext();) {
        // RelGroupObjFunctionValue groupFunction = (RelGroupObjFunctionValue)
        // iterator.next();
        // if (groupFunction.getFuncId() != null
        // && groupFunction.getGroupId() != null
        // && groupFunction.getFuncId().equals(objFuncId)
        // && groupFunction.getGroupId().equals(groupId)
        // && groupFunction.getObjId().equals(new
        // Integer(Integer.parseInt(objId)))
        // && groupFunction.getObjType().equals(new
        // Integer(Integer.parseInt(obj_type)))) {
        // return true;
        // }
        // }
        // }
        // }
        // //getOrgObjFunc
        // List ouList = getCache(organizationCache,
        // UserCenterConstant.ORGUSER);
        // for (Iterator iter = ouList.iterator(); iter.hasNext();) {
        // RelOrgUserValue orgUser = (RelOrgUserValue) iter.next();
        // if (orgUser != null && orgUser.getUserId() != null
        // && orgUser.getUserId().equals(userId)) {
        // Integer orgId = orgUser.getOrgId();
        // List obfList =
        // getCache(objFunctionCache,UserCenterConstant.RELORGOBJFUNCTION);
        // for (Iterator iterator = obfList.iterator(); iterator.hasNext();) {
        // RelOrgObjFunctionValue orgFunction = (RelOrgObjFunctionValue)
        // iterator.next();
        // if (orgFunction.getFuncId() != null
        // && orgFunction.getOrgId()!= null
        // && orgFunction.getFuncId().equals(objFuncId)
        // && orgFunction.getOrgId().equals(orgId)
        // && orgFunction.getObjId().equals(new
        // Integer(Integer.parseInt(objId)))
        // && orgFunction.getObjType().equals(new
        // Integer(Integer.parseInt(obj_type)))) {
        // return true;
        // }
        // }
        // }
        // }
        // return false;
    }

    /**
     * ��֤�û���Ĺ���Ȩ��
     * 
     * @param groupName
     * @param functionName
     * @return
     */
    public static boolean validateUserGroupFunction(String groupName, String functionName)
    {
        Cache userGroupCache = cacheManager.getCache(UserCenterConstant.USERGROUPCACHE);
        if (userGroupCache == null)
        {
            logger.error("userGroupCache cache hadn't defined in ehcahe.xml ");
            return false;
        }
        List group = getCache(userGroupCache, UserCenterConstant.USERGROUP);
        Integer groupId = null;
        for (Iterator iter = group.iterator(); iter.hasNext();)
        {
            UserGroupValue groupValue = (UserGroupValue)iter.next();
            if (groupValue.getName() != null && groupValue.getName().equals(groupName))
            {
                groupId = groupValue.getId();
                break;
            }
        }
        Cache functionCache = cacheManager.getCache(UserCenterConstant.FUNCTIONSCACHE);
        List funcList = getCache(functionCache, UserCenterConstant.FUNCTIONS);
        Integer funcId = null;
        for (int i = 0; i < funcList.size(); i++)
        {
            FunctionsValue funcValue = (FunctionsValue)funcList.get(i);
            if (funcValue.getName() != null && funcValue.getName().equals(functionName))
            {
                funcId = funcValue.getId();
                break;
            }
        }
        if (groupId == null || funcId == null)
        {
            return false;
        }

        List groupFunction = getCache(functionCache, UserCenterConstant.RELGROUPFUNCTION);
        for (Iterator iter = groupFunction.iterator(); iter.hasNext();)
        {
            RelGroupFunctionValue groupFunctionValue = (RelGroupFunctionValue)iter.next();
            if (groupFunctionValue.getFuncId() != null && groupFunctionValue.getGroupId() != null
                    && groupFunctionValue.getFuncId().equals(funcId) && groupFunctionValue.getGroupId().equals(groupId))
            {
                return true;
            }
        }
        return false;
    }

    private static Integer getFuncitonId(String functionName)
    {
        Cache functionCache = cacheManager.getCache(UserCenterConstant.FUNCTIONSCACHE);
        List funcList = getCache(functionCache, UserCenterConstant.FUNCTIONS);
        Integer funcId = null;
        for (int i = 0; i < funcList.size(); i++)
        {
            FunctionsValue funcValue = (FunctionsValue)funcList.get(i);
            if (funcValue.getName() != null && funcValue.getName().equals(functionName))
            {
                funcId = funcValue.getId();
                break;
            }
        }
        return funcId;
    }

    /**
     * ��֤�û���Ķ�����Ȩ��
     * 
     * @param groupName
     * @param functionName
     * @return
     */
    public static boolean validateUserGroupObjFunction(String groupName, String functionName, String objId, String obj_type)
    {
        Cache userGroupCache = cacheManager.getCache(UserCenterConstant.USERGROUPCACHE);
        if (userGroupCache == null)
        {
            logger.error("userGroupCache cache hadn't defined in ehcahe.xml ");
            return false;
        }
        List group = getCache(userGroupCache, UserCenterConstant.USERGROUP);
        Integer groupId = null;
        for (Iterator iter = group.iterator(); iter.hasNext();)
        {
            UserGroupValue groupValue = (UserGroupValue)iter.next();
            if (groupValue.getName() != null && groupValue.getName().equals(groupName))
            {
                groupId = groupValue.getId();
                break;
            }
        }

        Cache objFunctionCache = cacheManager.getCache(UserCenterConstant.OBJFUNCTIONCACHE);
        Integer objFuncId = null;
        List ofList = getCache(objFunctionCache, UserCenterConstant.OBJFUNCTION);
        for (Iterator iter = ofList.iterator(); iter.hasNext();)
        {
            ObjFunctionValue objfunction = (ObjFunctionValue)iter.next();
            if (functionName != null && objfunction.getName() != null && objfunction.getName().equals(functionName))
            {
                objFuncId = objfunction.getId();
                break;
            }
        }
        if (groupId == null || objFuncId == null)
        {
            return false;
        }

        List groupObjFunction = getCache(objFunctionCache, UserCenterConstant.RELGROUPOBJFUCNTION);
        for (Iterator iter = groupObjFunction.iterator(); iter.hasNext();)
        {
            RelGroupObjFunctionValue groupObjFunctionValue = (RelGroupObjFunctionValue)iter.next();
            if (groupObjFunctionValue.getFuncId() != null && groupObjFunctionValue.getGroupId() != null
                    && groupObjFunctionValue.getFuncId().equals(objFuncId) && groupObjFunctionValue.getGroupId().equals(groupId)
                    && groupObjFunctionValue.getObjId().equals(new Integer(objId))
                    && groupObjFunctionValue.getObjType().equals(new Integer(obj_type)))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * ��֤��֯�Ĺ���Ȩ��
     * 
     * @param groupName
     * @param functionName
     * @return
     */
    public static boolean validateOrgFunction(String orgName, String functionName)
    {
        Cache cache = cacheManager.getCache(UserCenterConstant.ORGANIZATIONCACHE);
        if (cache == null)
        {
            logger.error("ORGANIZATION cache hadn't defined in ehcahe.xml ");
            return false;
        }
        List org = getCache(cache, UserCenterConstant.ORGANIZATION);
        Integer orgId = getOrgIdByOrgName(org, orgName);
        Cache functionCache = cacheManager.getCache(UserCenterConstant.FUNCTIONSCACHE);
        Integer funcId = getFuncitonId(functionName);
        if (orgId == null || funcId == null)
        {
            return false;
        }
        List orgFunction = getCache(functionCache, UserCenterConstant.RELORGFUNCTION);
        for (Iterator iter = orgFunction.iterator(); iter.hasNext();)
        {
            RelOrgFunctionValue orgFunctionValue = (RelOrgFunctionValue)iter.next();
            if (orgFunctionValue.getFuncId() != null && orgFunctionValue.getOrgId() != null && orgFunctionValue.getFuncId().equals(funcId)
                    && orgFunctionValue.getOrgId().equals(orgId))
            {
                return true;
            }
        }
        return false;
    }

    private static Integer getOrgIdByOrgName(List orglist, String orgName)
    {
        Integer orgId = null;
        for (Iterator iter = orglist.iterator(); iter.hasNext();)
        {
            OrganizationValue orgValue = (OrganizationValue)iter.next();
            if (orgValue.getName() != null && orgValue.getName().equals(orgName))
            {
                orgId = orgValue.getId();
                break;
            }
        }
        return orgId;
    }

    /**
     * ��֤�û���Ķ�����Ȩ��
     * 
     * @param groupName
     * @param functionName
     * @return
     */
    public static boolean validateOrgObjFunction(String orgName, String functionName, String objId, String obj_type)
    {
        Cache cache = cacheManager.getCache(UserCenterConstant.ORGANIZATIONCACHE);
        if (cache == null)
        {
            logger.error("ORGANIZATION cache hadn't defined in ehcahe.xml ");
            return false;
        }
        List org = getCache(cache, UserCenterConstant.ORGANIZATION);
        Integer orgId = getOrgIdByOrgName(org, orgName);

        Cache objFunctionCache = cacheManager.getCache(UserCenterConstant.OBJFUNCTIONCACHE);
        Integer objFuncId = null;
        List ofList = getCache(objFunctionCache, UserCenterConstant.OBJFUNCTION);
        for (Iterator iter = ofList.iterator(); iter.hasNext();)
        {
            ObjFunctionValue objfunction = (ObjFunctionValue)iter.next();
            if (functionName != null && objfunction.getName() != null && objfunction.getName().equals(functionName))
            {
                objFuncId = objfunction.getId();
                break;
            }
        }
        if (orgId == null || objFuncId == null)
        {
            return false;
        }

        List orgObjFunction = getCache(objFunctionCache, UserCenterConstant.RELORGOBJFUNCTION);
        for (Iterator iter = orgObjFunction.iterator(); iter.hasNext();)
        {
            RelOrgObjFunctionValue orgObjFunctionValue = (RelOrgObjFunctionValue)iter.next();
            if (orgObjFunctionValue.getFuncId() != null && orgObjFunctionValue.getOrgId() != null && orgObjFunctionValue.getObjId() != null
                    && orgObjFunctionValue.getObjType() != null && orgObjFunctionValue.getFuncId().equals(objFuncId)
                    && orgObjFunctionValue.getObjType().equals(new Integer(obj_type))
                    && orgObjFunctionValue.getObjId().equals(new Integer(objId)) && orgObjFunctionValue.getOrgId().equals(orgId))
            {
                return true;
            }
        }
        return false;
    }

    private static List getCache(Cache cache, String constant)
    {
        Element element = cache.get(constant);
        if (element == null)
        {
            logger.error(constant + " not in cache");
            return new ArrayList();
        }
        List list = (List)element.getObjectValue();
        return list;
    }

    private static Integer getUserIdByAccount(List userList, String userAccount)
    {
        Integer userId = null;
        for (int i = 0; userList != null && i < userList.size(); i++)
        {
            UserValue userValue = (UserValue)userList.get(i);
            if (userValue.getAccount() != null && userValue.getAccount().equals(userAccount))
            {
                userId = userValue.getId();
                break;
            }
        }
        return userId;
    }

    private static HashMap userObjMap = new HashMap();

    public synchronized static HashMap computeObjFunction()
    {
        Cache objFunctionCache = cacheManager.getCache(UserCenterConstant.OBJFUNCTIONCACHE);
        Cache userGroupCache = cacheManager.getCache(UserCenterConstant.USERGROUPCACHE);
        if (objFunctionCache == null)
        {
            logger.error("objFunctionCache cache hadn't defined in ehcahe.xml ");
        }
        if (userGroupCache == null)
        {
            logger.error("userGroupCache cache hadn't defined in ehcahe.xml ");
        }
        HashMap resultMap = new HashMap();
        List userList = Orgnization.getALlUser();
        // logger.debug("userList's size is:" + ((userList == null)?0:userList.size()));
        for (int j = 0; userList != null && j < userList.size(); j++)
        {
            UserValue userValue = (UserValue)userList.get(j);
            String userAccount = userValue.getAccount();
            Integer userId = userValue.getId();
            // logger.debug(""+ j + "compute for userid " + userId );
            List uofList = getCache(objFunctionCache, UserCenterConstant.RELUSEROBJFUNCTION);
            // �����û�Ȩ��
            for (int i = 0; uofList != null && i < uofList.size(); i++)
            {
                String funcName = null;
                RelUserObjFunctionValue userObj = (RelUserObjFunctionValue)uofList.get(i);
                if (userId.equals(userObj.getUserId()))
                {
                    Integer objId = userObj.getObjId();
                    Integer objType = userObj.getObjType();
                    Integer funcId = userObj.getFuncId();
                    funcName = getObjFunctionName(objFunctionCache, funcId);
                    if (funcName == null || userAccount == null)
                    {
                        continue;
                    }
                    resultMap.put("" + userAccount + "_" + objId + "_" + objType + "_" + funcName, "true");
                }
                // logger.debug("-----"+ j + "compute user privlege for userid " + userId + "," + funcName);
            }

            // �����û���Ȩ��
            List groupIdList = Orgnization.getUserGroupIdsByUserId(userId);
            for (int i = 0; groupIdList != null && i < groupIdList.size(); i++)
            {
                Integer integer = (Integer)groupIdList.get(i);
                List gofList = getCache(objFunctionCache, UserCenterConstant.RELGROUPOBJFUCNTION);
                String funcName = null;
                for (int x = 0; gofList != null && x < gofList.size(); x++)
                {
                    RelGroupObjFunctionValue relGroupObjFunction = (RelGroupObjFunctionValue)gofList.get(x);
                    Integer objId = relGroupObjFunction.getObjId();
                    Integer objType = relGroupObjFunction.getObjType();
                    Integer funcId = relGroupObjFunction.getFuncId();
                    funcName = getObjFunctionName(objFunctionCache, funcId);
                    if (funcName == null || userAccount == null)
                    {
                        continue;
                    }
                    if (integer.equals(relGroupObjFunction.getGroupId()))
                    {
                        resultMap.put("" + userAccount + "_" + objId + "_" + objType + "_" + funcName, "true");
                    }
                }
                // logger.debug("-----"+ j + "compute group privlege for userid " + userId + ",group : " + integer);
            }
            // ������֯�û�Ȩ��
            List orgList = Orgnization.getOrgnizationsByUserId(userId);
            for (int i = 0; orgList != null && i < orgList.size(); i++)
            {
                Integer integer = (Integer)orgList.get(i);
                List obfList = getCache(objFunctionCache, UserCenterConstant.RELORGOBJFUNCTION);
                for (int x = 0; obfList != null && x < obfList.size(); x++)
                {
                    RelOrgObjFunctionValue value = (RelOrgObjFunctionValue)obfList.get(x);
                    Integer funcId = value.getFuncId();
                    Integer objId = value.getObjId();
                    Integer objType = value.getObjType();
                    String funcName = getObjFunctionName(objFunctionCache, funcId);
                    if (funcName == null || userAccount == null)
                    {
                        continue;
                    }
                    if (integer.equals(value.getOrgId()))
                    {
                        resultMap.put("" + userAccount + "_" + objId + "_" + objType + "_" + funcName, "true");
                    }
                }
                // logger.debug("-----"+ j + "compute org privlege for userid " + userId + ",org : " + integer);
            }
        }
        return resultMap;
        // //�����û�������Ȩ�޶�������
        // setUserObjMap(resultMap);
    }

    private static String getObjFunctionName(Cache objFunctionCache, Integer funcId)
    {
        List ofList = getCache(objFunctionCache, UserCenterConstant.OBJFUNCTION);
        for (Iterator iter = ofList.iterator(); iter.hasNext();)
        {
            ObjFunctionValue objfunction = (ObjFunctionValue)iter.next();
            if (objfunction.getId().equals(funcId))
            {
                return objfunction.getName();
            }
        }
        return null;
    }

    public static void main(String[] args)
    {
        PropertyConfigurator.configure("usercenter/WEB-INF/conf/log4j.properties");
        // boolean s = Function.validateObjFunction("ssss", "addObj","1","1");
        // System.out.println(s);

    }

    /**
     * @return userObjMap
     */
    public synchronized static HashMap getUserObjMap()
    {
        return userObjMap;
    }

    /**
     * @param userObjMap Ҫ���õ� userObjMap
     */
    public synchronized static void setUserObjMap(HashMap userObjMap)
    {
        Function.userObjMap = userObjMap;
    }

    /**
     * @param userAccount
     * @param ObjFunctionName
     * @return
     */
    public static List getObjByUserAndFunction(String userAccount, String ObjFunctionName)
    {
        List resultList = new ArrayList();
        Cache userCache = cacheManager.getCache(UserCenterConstant.USERCACHE);
        Cache objFunctionCache = cacheManager.getCache(UserCenterConstant.OBJFUNCTIONCACHE);

        if (userCache == null)
        {
            logger.error("UserCache cache hadn't defined in ehcahe.xml ");
            return null;
        }
        if (objFunctionCache == null)
        {
            logger.error("objFunctionCache cache hadn't defined in ehcahe.xml ");
            return null;
        }
        List userList = getCache(userCache, UserCenterConstant.USER);
        Integer userId = getUserIdByAccount(userList, userAccount);
        Integer funcId = null;
        List ofList = getCache(objFunctionCache, UserCenterConstant.OBJFUNCTION);
        for (Iterator iter = ofList.iterator(); iter.hasNext();)
        {
            ObjFunctionValue objfunction = (ObjFunctionValue)iter.next();
            if (objfunction.getName().equals(ObjFunctionName))
            {
                funcId = objfunction.getId();
                break;
            }
        }
        if (userId != null && funcId != null)
        {
            HashMap existMap = new HashMap();
            List userObjFunctionList = getCache(objFunctionCache, UserCenterConstant.RELUSEROBJFUNCTION);
            for (int i = 0; userObjFunctionList != null && i < userObjFunctionList.size(); i++)
            {
                RelUserObjFunctionValue relUserObjFunction = (RelUserObjFunctionValue)userObjFunctionList.get(i);
                Integer objId = relUserObjFunction.getObjId();
                Integer objType = relUserObjFunction.getObjType();
                if (relUserObjFunction.getFuncId().equals(funcId) && relUserObjFunction.getUserId().equals(userId))
                {
                    if (!existMap.containsKey(objId + "_" + objType))
                    {
                        resultList.add(relUserObjFunction);
                        existMap.put(objId + "_" + objType, "true");
                    }
                }
            }
            List groupIdList = Orgnization.getUserGroupIdsByUserId(userId);
            List gofList = getCache(objFunctionCache, UserCenterConstant.RELGROUPOBJFUCNTION);
            for (int i = 0; groupIdList != null && i < groupIdList.size(); i++)
            {
                Integer integer = (Integer)groupIdList.get(i);
                for (int x = 0; gofList != null && x < gofList.size(); x++)
                {
                    RelGroupObjFunctionValue relGroupObjFunction = (RelGroupObjFunctionValue)gofList.get(x);
                    Integer objId = relGroupObjFunction.getObjId();
                    Integer objType = relGroupObjFunction.getObjType();
                    if (relGroupObjFunction.getGroupId().equals(integer) && relGroupObjFunction.getFuncId().equals(funcId))
                    {
                        RelUserObjFunctionValue relUserObjFunction = new RelUserObjFunctionValue();
                        relUserObjFunction.setFuncId(funcId);
                        relUserObjFunction.setObjId(objId);
                        relUserObjFunction.setUserId(userId);
                        relUserObjFunction.setObjType(objType);
                        if (!existMap.containsKey(objId + "_" + objType))
                        {
                            resultList.add(relUserObjFunction);
                            existMap.put(objId + "_" + objType, "true");
                        }
                    }
                }
            }
            List orgList = Orgnization.getOrgnizationsByUserId(userId);
            List oroList = getCache(objFunctionCache, UserCenterConstant.RELORGOBJFUNCTION);
            for (int i = 0; orgList != null && i < orgList.size(); i++)
            {
                Integer integer = (Integer)orgList.get(i);
                for (int x = 0; oroList != null && x < oroList.size(); x++)
                {
                    RelOrgObjFunctionValue relOrgObjFunctionValue = (RelOrgObjFunctionValue)oroList.get(x);
                    Integer objId = relOrgObjFunctionValue.getObjId();
                    Integer objType = relOrgObjFunctionValue.getObjType();
                    if (relOrgObjFunctionValue.getOrgId().equals(integer) && relOrgObjFunctionValue.getFuncId().equals(funcId))
                    {
                        RelUserObjFunctionValue relUserObjFunction = new RelUserObjFunctionValue();
                        relUserObjFunction.setFuncId(funcId);
                        relUserObjFunction.setObjId(objId);
                        relUserObjFunction.setUserId(userId);
                        relUserObjFunction.setObjType(objType);
                        if (!existMap.containsKey(objId + "_" + objType))
                        {
                            resultList.add(relUserObjFunction);
                            existMap.put(objId + "_" + objType, "true");
                        }
                    }
                }
            }

        }
        return resultList;
    }
}
